﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseEpub
{
    public class GetImageTool
    {
        public string SaveImage(byte[] imageBytes, string imgType)
        {

            //string cms = System.Configuration.ConfigurationManager.AppSettings["cmsWebsite"];
            //if (string.IsNullOrEmpty(cms)) return null;
            //string newCms = cms.Replace("https", "http");
            //var token = HexStudy.WebCommon.Authen.Token.GetToken(CurrentUser.Name);
            string url = "http://cms.ld.hexstudy.com" + "/FileManagement/UpLoadFile_Server?ssotoken=69D9701A6E0DBC48597201653442D9E09F7CF361EDC5FC63C5D6C70EE2608CC5612E35A657D4EE05D517660D1385BB4551273C8361ECE160E484FF512D87FDA0956CF5CC977616CBE47C415061D60A6066B792C0DE00F06A1BF3835FB235735A";
            System.Net.HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp(url); //System.Net.HttpWebRequest.Create(url);
            request.Method = "POST";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.Headers.Add("ext", imgType);
            request.Headers.Add("contentType", "image/" + imgType);
            request.Headers.Add("hash", Guid.NewGuid().ToString());
            //填充POST数据
            request.ContentLength = imageBytes.Length;
            //myReq.
            System.IO.Stream requestStream = request.GetRequestStream();
            requestStream.Write(imageBytes, 0, imageBytes.Length);
            requestStream.Close();
            string result = string.Empty;
            try
            {
                //发送POST数据请求服务器
                System.Net.HttpWebResponse HttpWResp = (System.Net.HttpWebResponse)request.GetResponse();

                System.IO.Stream myStream = HttpWResp.GetResponseStream();

                //获取服务器返回信息
                System.IO.StreamReader reader = new System.IO.StreamReader(myStream, System.Text.Encoding.UTF8);
                result = reader.ReadToEnd();


                //释放
                myStream.Close();

            }
            catch (Exception exp)
            {
                result = "报错：" + exp.Message;
            }

            return result;
        }

        /// <summary>
        /// 获取图片的byte数组
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        public byte[] GetByte(string imgPath)
        {
            FileStream fs = File.OpenRead(imgPath); //OpenRead
            int filelength = 0;
            filelength = (int)fs.Length; //获得文件长度 
            Byte[] image = new Byte[filelength]; //建立一个字节数组
            fs.Read(image, 0, filelength); //按字节流读取
            fs.Seek(0, SeekOrigin.Begin);
            //System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
            fs.Close();
            return image;
            //Bitmap bit = new Bitmap(result);
        }

        /// <summary>
        /// 获取图片id
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        public Int64 GetImgId(string imgPath)
        {
            byte[] imgBytes = GetByte(imgPath);
            string imgType = BaseEpub.ReplacePageImg.ImageTypeCheck.CheckImageTypeName(imgPath);
            string result = SaveImage(imgBytes, imgType);

            BaseEpub.ReplacePageImg.UpLoadBack upLoadBack = JsonConvert.DeserializeObject<BaseEpub.ReplacePageImg.UpLoadBack>(result);
            //图片id
            return upLoadBack.ResourceID;
        }
    }
}
